/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmCustomerBrower.java
 *
 * Created on May 27, 2009, 10:28:04 AM
 */
package inventorymanagement;

import DataAccess.TableAccess;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;

/**
 *
 * @author Home
 */
public class frmCustomerBrower extends javax.swing.JFrame {

    static frmProcedureBill code;

    /** Creates new form frmCustomerBrower */
    public frmCustomerBrower() {
        initComponents();
        refresh();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        tblCustomer = new javax.swing.JTable(){
            public boolean isCellEditable(int rowIndex, int colIndex) {
                return false;   //Disallow the editing of any cell
            }
        };
        tblCustomer.getTableHeader().setReorderingAllowed(false);
        jLabel2 = new javax.swing.JLabel();
        txtSearchName = new javax.swing.JTextField();
        txtSearhCustomerID = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        btnSearchRefresh = new javax.swing.JButton();
        btnSelect = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Customer Browse");
        setResizable(false);

        tblCustomer.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        tblCustomer.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblCustomerMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tblCustomer);

        jLabel2.setText("Customer Name:");

        txtSearchName.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtSearchNameKeyReleased(evt);
            }
        });

        txtSearhCustomerID.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtSearhCustomerIDKeyReleased(evt);
            }
        });

        jLabel1.setText("Customer ID:");

        btnSearchRefresh.setText("Refresh");
        btnSearchRefresh.setPreferredSize(new java.awt.Dimension(100, 23));
        btnSearchRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchRefreshActionPerformed(evt);
            }
        });

        btnSelect.setText("Select");
        btnSelect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSelectActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 440, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel2)
                            .add(jLabel1))
                        .add(18, 18, 18)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(txtSearchName)
                            .add(txtSearhCustomerID, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 187, Short.MAX_VALUE))
                        .add(37, 37, 37)
                        .add(btnSearchRefresh, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(18, 18, 18))
                    .add(btnSelect))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(txtSearhCustomerID, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel1)
                    .add(btnSearchRefresh, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel2)
                    .add(txtSearchName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(14, 14, 14)
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 200, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(btnSelect)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtSearchNameKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtSearchNameKeyReleased
        String str = txtSearchName.getText();
        String sql = "";
        if (str.equals("")) {
            sql = "select CustomerID,CustomerName,Address from Customers from Customers";
        } else {
            sql = ("select CustomerID,CustomerName,Address from Customers where CustomerName like '%" + str + "%'");
        }
        try {
            TableAccess.readToTable(sql, tblCustomer);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }//GEN-LAST:event_txtSearchNameKeyReleased

    private void txtSearhCustomerIDKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtSearhCustomerIDKeyReleased
        String str = txtSearhCustomerID.getText();
        String sql = "";
//        if (str.equals("")) {
//            sql = "select CustomerID,CustomerName,Address from Customers";
//        }
//        if (interact.iCheckForm.checkNumber(str)) {
//            JOptionPane.showMessageDialog(this, "Invalid fomat", "Error", JOptionPane.ERROR_MESSAGE);
//            return;
//        } else {
//            sql = ("select CustomerID,CustomerName,Address from Customers where CustomerID=" + Integer.valueOf(str));
//        }
        if (str.equals("")) {
            sql = "select CustomerID,CustomerName,Address from Customers";
        } else {
            sql = "select CustomerID,CustomerName,Address from Customers where CustomerID like '%" + str + "%'";
        }
        try {
            TableAccess.readToTable(sql, tblCustomer);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }//GEN-LAST:event_txtSearhCustomerIDKeyReleased

    private void btnSearchRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchRefreshActionPerformed
        refresh();
    }//GEN-LAST:event_btnSearchRefreshActionPerformed

    private void btnSelectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSelectActionPerformed
        int cusID = tblCustomer.getSelectedRow();
        if (cusID == -1) {
            JOptionPane.showMessageDialog(this, "You must choose a item");
            return;
        } else {
            int row = tblCustomer.getSelectedRow();
            code.txtCustomerID.setText(tblCustomer.getValueAt(row, 0).toString());
            this.dispose();
        }
    }//GEN-LAST:event_btnSelectActionPerformed

    private void tblCustomerMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblCustomerMouseClicked
        // TODO add your handling code here:
        int row = tblCustomer.getSelectedRow();
        String str = tblCustomer.getValueAt(row, 0).toString();
        String str1 = tblCustomer.getValueAt(row, 1).toString();
        txtSearhCustomerID.setText(str);
        txtSearchName.setText(str1);
    }//GEN-LAST:event_tblCustomerMouseClicked
    private void refresh() {
        txtSearhCustomerID.setText("");
        txtSearchName.setText("");
        String sql = ("select CustomerID,CustomerName,Address from Customers");
        try {
            TableAccess.readToTable(sql, tblCustomer);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

//    private boolean isNumberic(String value) {
//        Pattern pattern = Pattern.compile("\\d+");
//        Matcher matcher = pattern.matcher(value);
//        if (!matcher.find()) {
//            return false;
//        } else {
//            return true;
//        }
//    }
    /**
     * @param args the command line arguments
     */
    /*public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new frmCustomerBrower().setVisible(true);
    }
    });
    }*/
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSearchRefresh;
    private javax.swing.JButton btnSelect;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tblCustomer;
    private javax.swing.JTextField txtSearchName;
    private javax.swing.JTextField txtSearhCustomerID;
    // End of variables declaration//GEN-END:variables
}
